﻿@page
@using SXFramWork.Web
@{
    ViewData["Title"] = "部门管理";
    Layout = AdminLayout.Pjax(HttpContext);
}
    <div id="container">
        <div class="layui-col-220">
            <div class="layui-nav-title">部门管理</div>
            <div id="tree" class="layui-tree-cus" style="overflow:auto;"></div>
        </div>
        <div class="right-col-body">
            <div class="list-wall">
                <div class="layui-form list-search">
                    <button type="button" class="layui-btn layui-btn-normal layui-hide" data-type="reload">
                        <i class="layui-icon layui-icon-refresh"></i> 刷新
                    </button>

                    <button type="button" class="layui-btn layui-btn-normal" data-type="Add"><i class="layui-icon"></i> 添加</button>
                    <button type="button" class="layui-btn layui-btn-danger" data-type="Delete"><i class="layui-icon"></i> 删除</button>
                </div>
                <table class="layui-hide" id="tablelist" lay-filter="tool"></table>
            </div>
        </div>

        <script type="text/html" id="tool">
            <a class="layui-btn layui-btn-warm" lay-event="edit"> 修改</a>
        </script>

        <script>
            layui.use(['table', 'layer', 'jquery', 'common', 'tree'],
                function () {
                    var table = layui.table,
                        layer = layui.layer,
                        $ = layui.jquery,
                        os = layui.common,
                        tree = layui.tree;
                    $("#tree").css({ 'height': $(window).height() - 140 });
                    os.tableRender({
                        elem: '#tablelist',
                        url: '/api/admin/sysorganize/list',
                        cols: [
                            [
                                { type: 'radio', fixed: 'left' },
                                { field: 'name', title: '部门名称', sort: true, fixed: 'left' },
                                { field: 'parentName', title: '父节点名称' },
                                { field: 'sort', width: 150, title: '排序', sort: true },
                                { width: 150, title: '操作', templet: '#tool' }
                            ]
                        ]
                    });

                    var id = '', active = {

                        initTree() {
                            os.ajax('api/admin/sysorganize/gettree', {}, function (res) {
                                tree.render({
                                    elem: '#tree'
                                    ,id:'tid'
                                    , data: res.data
                                    , onlyIconControl: true
                                    , click: function (obj) {
                                        id = obj.data.id;
                                        active.reload();
                                    }
                                });
                            },'get');
                        },
                        reload: function () {
                            os.tableReload('tablelist',
                                { where: { 'id': id } }
                            );
                            os.ajax('api/admin/sysorganize/gettree', {}, function (res) {
                                tree.reload('tid', {
                                    data: res.data
                                }
                                )
                            }, 'get');


                        },
                       
                        Add: function () {
                            os.Open('添加部门', '/admin/sysorganize/add', '620px', '300px', function () {
                               
                            });
                        },
                        Delete: function () {
                            var checkStatus = table.checkStatus('tablelist')
                                , data = checkStatus.data;
                            if (data.length === 0) {
                                os.warning("请选择要删除的项目");
                                return;
                            }
                            var str = '';
                            $.each(data, function (i, item) {
                                str = item.id 
                            });
                            layer.confirm('确定要执行批量删除吗？', function (index) {
                                layer.close(index);
                                os.ajax('api/admin/sysorganize/delete/', str, function (res) {
                                   
                                    if (res.code === 1) {
                                        active.reload();
                                        os.success('删除成功！');
                                    } else {
                                        os.error(res.msg);
                                    }
                                });
                            });

                        }
                    };
                    active.initTree();
                    $('.list-search .layui-btn').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });

                    //监听工具条
                    table.on('tool(tool)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') {
                            os.Open('编辑部门', '/admin/sysorganize/add/?id=' + data.id, '620px', '300px', function () {
                               
                            })
                        }
                    });
                });</script>

    </div>

